package com.frat.controller;

import java.util.List;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BluetoothListAdapter extends ArrayAdapter<BluetoothDevice> {

	private final String TAG = this.getClass().getSimpleName();

	public BluetoothListAdapter(Context context, int textViewResourceId, List<BluetoothDevice> objects) {
		super(context, textViewResourceId, objects);
	}

	@Override
	public View getView (int position, View convertView, ViewGroup parent) {
		Holder holder;
		if(null == convertView) {

			LayoutInflater vi = LayoutInflater.from(getContext()); 
			convertView = vi.inflate(R.layout.bluetooth_list_item, null);


			holder = new Holder();
			holder.deviceName = (TextView) convertView.findViewById(R.id.device_name_tv);
			holder.deviceMac = (TextView) convertView.findViewById(R.id.device_mac_tv);
			holder.deviceUUIDs = (TextView) convertView.findViewById(R.id.device_uuids_tv);

			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		BluetoothDevice bDevice = getItem(position);

		Log.d(TAG, "Name: " + bDevice.getName());
		Log.d(TAG, "MAC: " + bDevice.getAddress());

		holder.deviceName.setText(bDevice.getName());
		holder.deviceMac.setText(bDevice.getAddress());

		ParcelUuid[] uuids = bDevice.getUuids();

		if(null != uuids) {
			for(ParcelUuid uuid:uuids) {
				Log.d(TAG, "UUIDS: " + uuid.toString());
				holder.deviceUUIDs.append(uuid.toString() + "\n");		

			}
		} else {
			Log.d(TAG, "No UUIDs found");
		}

		return convertView;
	}

	static class Holder {
		TextView deviceName;
		TextView deviceMac;
		TextView deviceUUIDs;
	}

}
